package com.crcode.crm.controller.log;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
/**
 * 日志控制层
 * @author lxw
 * @date 2019年4月9日
 */
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.crcode.crm.bean.log.DataLogBean;
import com.crcode.crm.service.log.DataLogService;
import com.crcode.crm.utils.MyFileUtil;
import com.crcode.crm.utils.PageVo;
import com.crcode.crm.utils.ResultPageUtil;
@Controller
@RequestMapping(value = "dataLog")
public class DataLogController {
	
	@Resource(name = "logService")
	private DataLogService logService;
	
	/**
	 * 分页查询
	 * @author lxw
	 * @date 2019年4月9日
	 * @param request
	 * @param param {"logType":"","startTime":"","endtTime":"","userUuid":"","searchWords":""}
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/findBySpec",method=RequestMethod.POST)
	@ResponseBody
	public ResultPageUtil findBySpec(HttpServletRequest request,@RequestParam Map<String, Object> param,PageVo vo){

		return this.logService.findBySpec(param, vo);
	}

	/**
	 * 根据id查询】
	 * 啊、@author lmz
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/findById",method = RequestMethod.POST)
	@ResponseBody
	public DataLogBean findLogById(HttpServletRequest request,Integer id){
		return  logService.findLogByid(id);
	}
	
	/**
	 * 导出表格
	 * @param request
	 * @param response
	 * @param session
	 * @throws Exception
	 */
	@ResponseBody
    @RequestMapping(value="/exportExcel", method = RequestMethod.POST)
	public void resetLiableUserByUuids(HttpServletRequest request,HttpServletResponse response,HttpSession session) throws Exception{
		String datas=request.getParameter("datas");
		JSONObject dataObj=JSONObject.parseObject(datas);
		PageVo vo=new PageVo();
		vo.setPageSize(Integer.parseInt(dataObj.getString("pageSize")));
		vo.setPageIndex(Integer.parseInt(dataObj.getString("pageIndex")));
		vo.setOrderBy(dataObj.getString("orderBy"));
		vo.setOrderDesc(Integer.parseInt(dataObj.getString("orderDesc")));
		Map<String, Object> param=new HashMap<>();
		param.put("searchWords", dataObj.getString("searchWords"));
		param.put("startTime", dataObj.getString("startTime"));
		param.put("endtTime", dataObj.getString("endtTime"));
		param.put("logType", dataObj.getString("logType"));
		param.put("detailedType", dataObj.getString("detailedType"));
		ResultPageUtil ru=logService.findBySpec(param, vo);
		Map<String,Object> map=new HashMap<>();
		map.put("rows", JSON.toJSONString(ru.getRows()));
		map.put("column", dataObj.getString("column"));
		map.put("type","log");
		try {
			MyFileUtil.exp(request, response, JSON.toJSONString(map));
		} catch (IOException e) {
			throw new Exception("导出文件错误",e);
		}
	}
}
